package com.example.socialapp.ui.search

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Tag
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue

/**
 * 搜索界面
 *
 * @param onNavigateBack 返回上一界面回调
 * @param onNavigateToUserProfile 导航到用户资料界面回调
 * @param onNavigateToPostDetail 导航到动态详情界面回调
 * @param onNavigateToTopic 导航到话题界面回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchScreen(
    onNavigateBack: () -> Unit,
    onNavigateToUserProfile: (String) -> Unit = {},
    onNavigateToPostDetail: (String) -> Unit = {},
    onNavigateToTopic: (String) -> Unit = {}
) {
    var searchQuery by remember { mutableStateOf("") }
    var isSearchActive by remember { mutableStateOf(false) }
    var selectedTabIndex by remember { mutableIntStateOf(0) }
    
    // 搜索历史和热搜数据（实际应用中应从数据库或ViewModel获取）
    val searchHistory = remember { 
        listOf("张三", "旅行", "美食推荐", "编程技巧", "健身") 
    }
    val hotSearches = remember { 
        listOf("热门电影", "流行音乐", "AI技术", "健康生活", "环保话题", "新冠疫情", "股市行情") 
    }
    
    // 搜索结果（根据实际搜索结果动态生成）
    val userResults = remember { generateSampleUsers(searchQuery, 5) }
    val postResults = remember { generateSamplePosts(searchQuery, 5) }
    val topicResults = remember { generateSampleTopics(searchQuery, 5) }
    
    // 搜索结果标签页
    val searchTabs = listOf("综合", "用户", "动态", "话题")
    
    Scaffold(
        topBar = {
            if (!isSearchActive) {
                TopAppBar(
                    title = { Text("搜索") },
                    navigationIcon = {
                        IconButton(onClick = onNavigateBack) {
                            Icon(
                                imageVector = Icons.Default.ArrowBack,
                                contentDescription = "返回"
                            )
                        }
                    },
                    colors = TopAppBarDefaults.topAppBarColors(
                        containerColor = MaterialTheme.colorScheme.primaryContainer,
                        titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                    )
                )
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            // 搜索框
            SearchBar(
                query = searchQuery,
                onQueryChange = { searchQuery = it },
                onSearch = { 
                    if (searchQuery.isNotBlank()) {
                        isSearchActive = true
                    }
                },
                onClear = { 
                    searchQuery = ""
                    isSearchActive = false
                }
            )
            
            if (!isSearchActive) {
                // 搜索历史和热搜
                SearchHome(
                    searchHistory = searchHistory,
                    hotSearches = hotSearches,
                    onHistoryItemClick = { 
                        searchQuery = it
                        isSearchActive = true
                    },
                    onHotSearchItemClick = { 
                        searchQuery = it
                        isSearchActive = true
                    },
                    onClearHistory = { /* 清除搜索历史逻辑 */ }
                )
            } else {
                // 搜索结果
                SearchResults(
                    selectedTabIndex = selectedTabIndex,
                    tabs = searchTabs,
                    onTabSelected = { selectedTabIndex = it },
                    userResults = userResults,
                    postResults = postResults,
                    topicResults = topicResults,
                    onUserClick = onNavigateToUserProfile,
                    onPostClick = onNavigateToPostDetail,
                    onTopicClick = onNavigateToTopic
                )
            }
        }
    }
}

/**
 * 搜索栏组件
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SearchBar(
    query: String,
    onQueryChange: (String) -> Unit,
    onSearch: () -> Unit,
    onClear: () -> Unit
) {
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(24.dp),
        color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 8.dp, vertical = 4.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = Icons.Default.Search,
                contentDescription = "搜索",
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.padding(start = 8.dp)
            )
            
            OutlinedTextField(
                value = query,
                onValueChange = onQueryChange,
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 8.dp),
                placeholder = { Text("搜索用户、动态、话题...") },
                singleLine = true,
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Text,
                    imeAction = ImeAction.Search
                ),
                keyboardActions = KeyboardActions(
                    onSearch = { onSearch() }
                ),
                colors = TextFieldDefaults.outlinedTextFieldColors(
                    focusedBorderColor = Color.Transparent,
                    unfocusedBorderColor = Color.Transparent,
                    cursorColor = MaterialTheme.colorScheme.primary
                )
            )
            
            if (query.isNotEmpty()) {
                IconButton(onClick = onClear) {
                    Icon(
                        imageVector = Icons.Default.Clear,
                        contentDescription = "清除搜索",
                        tint = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
            }
        }
    }
}

/**
 * 搜索主页（显示搜索历史和热搜）
 */
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun SearchHome(
    searchHistory: List<String>,
    hotSearches: List<String>,
    onHistoryItemClick: (String) -> Unit,
    onHotSearchItemClick: (String) -> Unit,
    onClearHistory: () -> Unit
) {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // 搜索历史
        item {
            Column {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Icon(
                            imageVector = Icons.Default.History,
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.primary
                        )
                        Spacer(modifier = Modifier.width(8.dp))
                        Text(
                            text = "搜索历史",
                            style = MaterialTheme.typography.titleMedium
                        )
                    }
                    
                    if (searchHistory.isNotEmpty()) {
                        IconButton(onClick = onClearHistory) {
                            Icon(
                                imageVector = Icons.Outlined.Delete,
                                contentDescription = "清除搜索历史",
                                tint = MaterialTheme.colorScheme.outline
                            )
                        }
                    }
                }
                
                Spacer(modifier = Modifier.height(8.dp))
                
                if (searchHistory.isEmpty()) {
                    Text(
                        text = "暂无搜索历史",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.outline
                    )
                } else {
                    FlowRow(
                        horizontalArrangement = Arrangement.spacedBy(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp)
                    ) {
                        searchHistory.forEach { item ->
                            SearchChip(
                                text = item,
                                onClick = { onHistoryItemClick(item) }
                            )
                        }
                    }
                }
            }
        }
        
        // 分隔线
        item {
            Divider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = MaterialTheme.colorScheme.outlineVariant
            )
        }
        
        // 热门搜索
        item {
            Column {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = Icons.Default.Search,
                        contentDescription = null,
                        tint = MaterialTheme.colorScheme.primary
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(
                        text = "热门搜索",
                        style = MaterialTheme.typography.titleMedium
                    )
                }
                
                Spacer(modifier = Modifier.height(16.dp))
                
                Column(
                    verticalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    hotSearches.forEachIndexed { index, item ->
                        HotSearchItem(
                            rank = index + 1,
                            text = item,
                            onClick = { onHotSearchItemClick(item) }
                        )
                    }
                }
            }
        }
    }
}

/**
 * 搜索词条组件
 */
@Composable
private fun SearchChip(
    text: String,
    onClick: () -> Unit
) {
    Surface(
        modifier = Modifier.clickable(onClick = onClick),
        shape = RoundedCornerShape(16.dp),
        color = MaterialTheme.colorScheme.surfaceVariant
    ) {
        Text(
            text = text,
            style = MaterialTheme.typography.bodyMedium,
            modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp)
        )
    }
}

/**
 * 热搜项组件
 */
@Composable
private fun HotSearchItem(
    rank: Int,
    text: String,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(vertical = 4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = rank.toString(),
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold,
            color = when (rank) {
                1 -> Color(0xFFE57373) // 红色
                2 -> Color(0xFFFFB74D) // 橙色
                3 -> Color(0xFFFFF176) // 黄色
                else -> MaterialTheme.colorScheme.outline
            },
            modifier = Modifier.width(32.dp)
        )
        
        Text(
            text = text,
            style = MaterialTheme.typography.bodyLarge,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )
    }
}

/**
 * 搜索结果组件
 */
@Composable
private fun SearchResults(
    selectedTabIndex: Int,
    tabs: List<String>,
    onTabSelected: (Int) -> Unit,
    userResults: List<UserResult>,
    postResults: List<PostResult>,
    topicResults: List<TopicResult>,
    onUserClick: (String) -> Unit,
    onPostClick: (String) -> Unit,
    onTopicClick: (String) -> Unit
) {
    Column(modifier = Modifier.fillMaxSize()) {
        // 选项卡
        TabRow(
            selectedTabIndex = selectedTabIndex,
            containerColor = MaterialTheme.colorScheme.surface
        ) {
            tabs.forEachIndexed { index, title ->
                Tab(
                    selected = selectedTabIndex == index,
                    onClick = { onTabSelected(index) },
                    text = { Text(text = title) }
                )
            }
        }
        
        // 结果列表
        when (selectedTabIndex) {
            0 -> { // 综合
                ComprehensiveResults(
                    userResults = userResults.take(2),
                    postResults = postResults.take(2),
                    topicResults = topicResults.take(2),
                    onUserClick = onUserClick,
                    onPostClick = onPostClick,
                    onTopicClick = onTopicClick,
                    onMoreUsersClick = { onTabSelected(1) },
                    onMorePostsClick = { onTabSelected(2) },
                    onMoreTopicsClick = { onTabSelected(3) }
                )
            }
            1 -> { // 用户
                UserResults(
                    users = userResults,
                    onUserClick = onUserClick
                )
            }
            2 -> { // 动态
                PostResults(
                    posts = postResults,
                    onPostClick = onPostClick
                )
            }
            3 -> { // 话题
                TopicResults(
                    topics = topicResults,
                    onTopicClick = onTopicClick
                )
            }
        }
    }
}

/**
 * 综合搜索结果组件
 */
@Composable
private fun ComprehensiveResults(
    userResults: List<UserResult>,
    postResults: List<PostResult>,
    topicResults: List<TopicResult>,
    onUserClick: (String) -> Unit,
    onPostClick: (String) -> Unit,
    onTopicClick: (String) -> Unit,
    onMoreUsersClick: () -> Unit,
    onMorePostsClick: () -> Unit,
    onMoreTopicsClick: () -> Unit
) {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(vertical = 8.dp)
    ) {
        // 用户结果
        if (userResults.isNotEmpty()) {
            item {
                ResultSection(
                    title = "用户",
                    icon = Icons.Default.Person,
                    showMore = userResults.size >= 2,
                    onMoreClick = onMoreUsersClick
                )
            }
            
            items(userResults) { user ->
                UserResultItem(
                    user = user,
                    onClick = { onUserClick(user.id) }
                )
            }
            
            item {
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
        }
        
        // 动态结果
        if (postResults.isNotEmpty()) {
            item {
                ResultSection(
                    title = "动态",
                    icon = Icons.Default.Search,
                    showMore = postResults.size >= 2,
                    onMoreClick = onMorePostsClick
                )
            }
            
            items(postResults) { post ->
                PostResultItem(
                    post = post,
                    onClick = { onPostClick(post.id) }
                )
            }
            
            item {
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
        }
        
        // 话题结果
        if (topicResults.isNotEmpty()) {
            item {
                ResultSection(
                    title = "话题",
                    icon = Icons.Default.Tag,
                    showMore = topicResults.size >= 2,
                    onMoreClick = onMoreTopicsClick
                )
            }
            
            items(topicResults) { topic ->
                TopicResultItem(
                    topic = topic,
                    onClick = { onTopicClick(topic.id) }
                )
            }
        }
    }
}

/**
 * 用户搜索结果列表
 */
@Composable
private fun UserResults(
    users: List<UserResult>,
    onUserClick: (String) -> Unit
) {
    if (users.isEmpty()) {
        EmptyResults(text = "没有找到相关用户")
    } else {
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(vertical = 8.dp)
        ) {
            items(users) { user ->
                UserResultItem(
                    user = user,
                    onClick = { onUserClick(user.id) }
                )
                Divider()
            }
        }
    }
}

/**
 * 用户结果项
 */
@Composable
private fun UserResultItem(
    user: UserResult,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 头像
        Box(
            modifier = Modifier
                .size(48.dp)
                .clip(CircleShape)
                .background(getColorForName(user.nickname)),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = user.nickname.first().toString(),
                style = MaterialTheme.typography.titleMedium,
                color = Color.White
            )
        }
        
        Spacer(modifier = Modifier.width(16.dp))
        
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = user.nickname,
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            Text(
                text = user.bio,
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }
}

/**
 * 动态搜索结果列表
 */
@Composable
private fun PostResults(
    posts: List<PostResult>,
    onPostClick: (String) -> Unit
) {
    if (posts.isEmpty()) {
        EmptyResults(text = "没有找到相关动态")
    } else {
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(vertical = 8.dp)
        ) {
            items(posts) { post ->
                PostResultItem(
                    post = post,
                    onClick = { onPostClick(post.id) }
                )
                Divider()
            }
        }
    }
}

/**
 * 动态结果项
 */
@Composable
private fun PostResultItem(
    post: PostResult,
    onClick: () -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(16.dp)
    ) {
        // 作者信息
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(
                text = post.authorName,
                style = MaterialTheme.typography.titleSmall,
                fontWeight = FontWeight.Bold
            )
            
            Spacer(modifier = Modifier.width(8.dp))
            
            Text(
                text = post.time,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.outline
            )
        }
        
        Spacer(modifier = Modifier.height(8.dp))
        
        // 内容
        Text(
            text = post.content,
            style = MaterialTheme.typography.bodyMedium,
            maxLines = 2,
            overflow = TextOverflow.Ellipsis
        )
    }
}

/**
 * 话题搜索结果列表
 */
@Composable
private fun TopicResults(
    topics: List<TopicResult>,
    onTopicClick: (String) -> Unit
) {
    if (topics.isEmpty()) {
        EmptyResults(text = "没有找到相关话题")
    } else {
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(vertical = 8.dp)
        ) {
            items(topics) { topic ->
                TopicResultItem(
                    topic = topic,
                    onClick = { onTopicClick(topic.id) }
                )
                Divider()
            }
        }
    }
}

/**
 * 话题结果项
 */
@Composable
private fun TopicResultItem(
    topic: TopicResult,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 话题图标
        Box(
            modifier = Modifier
                .size(48.dp)
                .clip(RoundedCornerShape(8.dp))
                .background(getColorForName(topic.name)),
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = Icons.Default.Tag,
                contentDescription = null,
                tint = Color.White,
                modifier = Modifier.size(24.dp)
            )
        }
        
        Spacer(modifier = Modifier.width(16.dp))
        
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = "#${topic.name}",
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            Text(
                text = "${topic.postsCount}条动态",
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
    }
}

/**
 * 结果分区标题
 */
@Composable
private fun ResultSection(
    title: String,
    icon: ImageVector,
    showMore: Boolean,
    onMoreClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.width(8.dp))
            
            Text(
                text = title,
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold
            )
        }
        
        if (showMore) {
            Text(
                text = "查看更多",
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.primary,
                modifier = Modifier.clickable(onClick = onMoreClick)
            )
        }
    }
}

/**
 * 空结果提示
 */
@Composable
private fun EmptyResults(text: String) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            style = MaterialTheme.typography.bodyLarge,
            color = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 根据名称生成颜色
 */
private fun getColorForName(name: String): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[name.hashCode().mod(colors.size).absoluteValue]
}

// 数据类

/**
 * 用户搜索结果数据类
 */
data class UserResult(
    val id: String,
    val nickname: String,
    val bio: String
)

/**
 * 动态搜索结果数据类
 */
data class PostResult(
    val id: String,
    val authorId: String,
    val authorName: String,
    val content: String,
    val time: String
)

/**
 * 话题搜索结果数据类
 */
data class TopicResult(
    val id: String,
    val name: String,
    val postsCount: Int
)

// 示例数据生成

/**
 * 生成示例用户搜索结果
 */
private fun generateSampleUsers(query: String, count: Int): List<UserResult> {
    // 实际应用中应从数据库或API获取搜索结果
    // 此处仅作演示
    val users = listOf(
        UserResult(
            id = "10086",
            nickname = "张三",
            bio = "热爱旅行和摄影，记录生活中的美好瞬间"
        ),
        UserResult(
            id = "10087",
            nickname = "李四",
            bio = "美食博主，分享各地美食和攻略"
        ),
        UserResult(
            id = "10088",
            nickname = "王五",
            bio = "科技爱好者，关注最新的科技动态"
        ),
        UserResult(
            id = "10089",
            nickname = "赵六",
            bio = "音乐创作者，热爱分享原创音乐作品"
        ),
        UserResult(
            id = "10090",
            nickname = "钱七",
            bio = "旅行达人，足迹遍布全国各地"
        )
    )
    
    // 根据搜索词过滤
    return if (query.isEmpty()) {
        users.take(count)
    } else {
        users.filter { 
            it.nickname.contains(query, ignoreCase = true) ||
            it.bio.contains(query, ignoreCase = true)
        }.take(count)
    }
}

/**
 * 生成示例动态搜索结果
 */
private fun generateSamplePosts(query: String, count: Int): List<PostResult> {
    // 实际应用中应从数据库或API获取搜索结果
    val posts = listOf(
        PostResult(
            id = "1",
            authorId = "10086",
            authorName = "张三",
            content = "今天去了云南大理，风景真的太美了！蓝天白云下的洱海像一面镜子，倒映着苍山的倩影。",
            time = "2小时前"
        ),
        PostResult(
            id = "2",
            authorId = "10087",
            authorName = "李四",
            content = "推荐一家超级好吃的火锅店，麻辣鲜香，特别过瘾。地址在市中心商场负一楼，周末人比较多要早点去。",
            time = "昨天"
        ),
        PostResult(
            id = "3",
            authorId = "10088",
            authorName = "王五",
            content = "最新的AI技术真是让人惊叹，已经能够生成如此逼真的图像和文本了。未来的发展令人期待！",
            time = "3天前"
        ),
        PostResult(
            id = "4",
            authorId = "10089",
            authorName = "赵六",
            content = "分享一首最近创作的钢琴曲，灵感来源于一个雨天的午后，希望大家喜欢。",
            time = "1周前"
        ),
        PostResult(
            id = "5",
            authorId = "10090",
            authorName = "钱七",
            content = "这是我在西藏拍摄的日落，金色的阳光洒在雪山上，美得不可思议。旅行是最好的投资！",
            time = "2周前"
        )
    )
    
    // 根据搜索词过滤
    return if (query.isEmpty()) {
        posts.take(count)
    } else {
        posts.filter { 
            it.authorName.contains(query, ignoreCase = true) ||
            it.content.contains(query, ignoreCase = true)
        }.take(count)
    }
}

/**
 * 生成示例话题搜索结果
 */
private fun generateSampleTopics(query: String, count: Int): List<TopicResult> {
    // 实际应用中应从数据库或API获取搜索结果
    val topics = listOf(
        TopicResult(
            id = "1",
            name = "旅行",
            postsCount = 12543
        ),
        TopicResult(
            id = "2",
            name = "美食",
            postsCount = 34256
        ),
        TopicResult(
            id = "3",
            name = "科技",
            postsCount = 8765
        ),
        TopicResult(
            id = "4",
            name = "音乐",
            postsCount = 15632
        ),
        TopicResult(
            id = "5",
            name = "摄影",
            postsCount = 23487
        )
    )
    
    // 根据搜索词过滤
    return if (query.isEmpty()) {
        topics.take(count)
    } else {
        topics.filter { 
            it.name.contains(query, ignoreCase = true)
        }.take(count)
    }
} 